package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/15.<br><br>
 *
 * 数字转换为十六进制数
 */
public class Topic405 {

    public static void main(String[] args) {
        Topic405 instance = new Topic405();

        System.out.println(instance.toHex(26));
        System.out.println(instance.toHex(-1));
        System.out.println(instance.toHex(0));
        System.out.println(instance.toHex(9));
    }

    public String toHex(int num) {
        if(num == 0) {
            return "0";
        }

        char[] map = new char[16];
        for(int i = 0; i < 10; i++) {
            map[i] = (char) ('0' + i);
        }

        for(int i = 0; i < 6; i++) {
            map[i + 10] = (char) ('a' + i);
        }

        StringBuilder builder = new StringBuilder();
        while (num != 0) {
            builder.append(map[num & 15]);
            num = num >>> 4;
        }

        return builder.reverse().toString();
    }
}
